package view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.administrator.text.R;
import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/7/13.
 *
 */
public class ViewLinearLayout extends LinearLayout{
    private Context mContext;
    private Drawable mBitmp;
    private String mString;

    @Bind(R.id.image_view_we) ImageView mImageView;  //左边的图标
    @Bind(R.id.text_view_we)  TextView mTextView;
    public ViewLinearLayout(Context context) {
        super(context);
        init();
    }

    public ViewLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        getAttrs(context, attrs);
        init();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void init() {
        View view = RelativeLayout.inflate(mContext, R.layout.view_we, null);
        //以下两句的顺序不能调换，要先addView，然后才能通过findViewById找到该TextView
        addView(view);
        ButterKnife.bind(this);
        mTextView.setText(mString);
        mImageView.setBackground(mBitmp);
    }


    /**
     * get attrs
     */
    private void getAttrs(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.we_view_linearlayout_attr);
        mString = ta.getString(R.styleable.we_view_linearlayout_attr_stringText);
        mBitmp = ta.getDrawable(R.styleable.we_view_linearlayout_attr_bitmap);
        ta.recycle();
    }

    /**
     * 改变 mTextView的值
     */
    public void setViewText(String string){
        mTextView.setText(string);
    }

    /**
     * 改变了 图片的样式
     */
    public void setIVBitmp(int i){
        mImageView.setBackgroundResource(i);
    }

    /**
     * 返回显示的内容
     */
    public String getViewString(){
        return mTextView.getText().toString();
    }

}
